import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";

export interface IGroupMoneyOption {
    groupCount: number;
    addStep: number;
    devideStep: number;
    reverse: boolean; //true:赢了+100，输了-100
    maxLevel: number;
}
export class NormalGroupMoneyStrategy implements IMoneyStrategy {
    option: IGroupMoneyOption;
    touchTop: boolean = false;
    strategyName: string;
    debugCompoenent: string = "";
    recmV: EnumBjlResult = EnumBjlResult.U;
    recmMoney: number = 0;
    orecmV: number = 0;
    orecmMoney: number = 0;
    constructor(name: string, option?: IGroupMoneyOption) {
        this.strategyName = name;
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                groupCount: 1,
                addStep: 100,
                devideStep: -100,
                reverse: false,
                maxLevel: 2000
            };
        }
    }
    showMoney(roundInfo?: RoundInfo): [EnumBjlResult, number] {
        if (roundInfo) {
            if (this.orecmV != EnumBjlResult.U && roundInfo.reverse) {
                this.recmV = this.orecmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
                this.recmMoney = this.orecmMoney;
            }
            else {
                this.recmV = this.orecmV;
                this.recmMoney = this.orecmMoney;
            }
        }

        return [this.recmV, this.recmMoney];
    }
    resetRecm(): void {
        this.resetStrategy();
    }
    resetStrategy(): void {
        this.touchTop = false;
        this.recmV = EnumBjlResult.U;
        this.recmMoney = 0;
        this.orecmV = EnumBjlResult.U;
        this.orecmMoney = 0;
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        if (recmV == EnumBjlResult.U)
            return [recmV, recmInfo.recmMoney];

        let recmMoney = 0;
        // if (userRecm.roundSummaryInfo.winCount == userRecm.roundSummaryInfo.failCount) {
        //     recmV = EnumBjlResult.U;
        //     recmMoney = baseMoney;
        // }
        // else 
        if (userRecm.roundSummaryInfo.winCount > userRecm.roundSummaryInfo.failCount) {
            recmV = roundInfo.userRecm;
            recmMoney = (userRecm.roundSummaryInfo.winCount - userRecm.roundSummaryInfo.failCount) * baseMoney;
        }
        else if (userRecm.roundSummaryInfo.winCount < userRecm.roundSummaryInfo.failCount) {
            recmV = roundInfo.userRecm == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
            recmMoney = (userRecm.roundSummaryInfo.failCount - userRecm.roundSummaryInfo.winCount) * baseMoney;
        }
        else {
            recmV = EnumBjlResult.U;
            recmMoney = 0;
        }
        if (recmMoney >= this.option.maxLevel) {
            recmMoney = this.option.maxLevel;
            this.touchTop = true;
        }
        if (this.option.reverse && recmV != EnumBjlResult.U) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }

        this.orecmV = recmV;
        this.orecmMoney = recmMoney;

        if (recmV != EnumBjlResult.U && roundInfo.reverse) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }
        this.recmV = recmV;
        this.recmMoney = recmMoney;
        return [recmV, recmMoney];
    }
    isTouchTop(): boolean {
        return this.touchTop;
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as NormalGroupMoneyStrategy;
        if (obj) {
            //复制
            this.isTouchTop = obj.isTouchTop;
            this.recmMoney = obj.recmMoney;
            this.recmV = obj.recmV;
            this.orecmV = obj.orecmV;
            this.orecmMoney = obj.orecmMoney;
        }
    }

}

export interface IGroupStepAddMoneyOption {
    winGroupCount: number;
    failGroupCount: number;
    upLimit: number;
    addStep: number;
    devideStep: number;
    reverse: boolean; //true:赢了+100，输了-100
    baseMoney: number;
}
export class GroupStepAddMoneyStrategy implements IMoneyStrategy {
    strategyName: string;
    debugCompoenent: string = "GroupStepAddMoneyStrategyDebug";
    option: IGroupStepAddMoneyOption;
    currentMoney: number;
    gameStart: boolean = true;

    recmV: number = EnumBjlResult.U;
    recmMoney: number = 0;
    orecmV: number = 0;
    orecmMoney: number = 0;
    constructor(name: string, option: IGroupStepAddMoneyOption) {
        this.currentMoney = 100;
        if (name) {
            this.strategyName = name;
        }
        else {
            this.strategyName = "";
        }

        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                winGroupCount: 10,
                failGroupCount: 10,
                upLimit: 1000,
                addStep: 100,
                devideStep: -100,
                reverse: false, //true:赢了+100，输了-100
                baseMoney: 100
            };
        }
    }
    showMoney(roundInfo?: RoundInfo): [EnumBjlResult, number] {
        if (roundInfo) {
            if (this.orecmV != EnumBjlResult.U && roundInfo.reverse) {
                this.recmV = this.orecmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
                this.recmMoney = this.orecmMoney;
            }
            else {
                this.recmV = this.orecmV;
                this.recmMoney = this.orecmMoney;
            }
        }

        return [this.recmV, this.recmMoney];
    }
    resetRecm(): void {
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {

        if (!this.gameStart) {
            return [EnumBjlResult.U, 0];
        }

        let recmMoney = this.currentMoney;
        let recmV = roundInfo.userRecm;
        let win = recmInfo.roundSummaryInfo.winCount;
        let fail = recmInfo.roundSummaryInfo.failCount
        let diff = win - fail;
        if (win == 0 && fail == 0) {

        }
        else if (diff == this.option.winGroupCount) {
            let prev = this.currentMoney
            this.currentMoney += this.option.devideStep;
            if (this.currentMoney >= this.option.baseMoney && prev > this.option.baseMoney) {
                recmInfo.roundSummaryInfo.resetSummaryInfo();
            }

        }
        else if (diff == (-this.option.failGroupCount)) {
            this.currentMoney += this.option.addStep;
            recmInfo.roundSummaryInfo.resetSummaryInfo();
        }
        else { }

        if (this.currentMoney > this.option.upLimit) {
            recmMoney = this.option.upLimit;
        }
        else if (this.currentMoney < this.option.baseMoney) {
            recmMoney = this.option.baseMoney;
            this.currentMoney = this.option.baseMoney;
        }
        else {
            recmMoney = this.currentMoney;
        }

        this.orecmV = recmV;
        this.orecmMoney = recmMoney;

        if (recmV != EnumBjlResult.U && this.option.reverse) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }

        this.recmV = recmV;
        this.recmMoney = recmMoney;

        return [recmV, recmMoney];
    }
    resetStrategy(): void {
        this.currentMoney = this.option.baseMoney;
        this.gameStart = true;

        this.recmV = EnumBjlResult.U;
        this.recmMoney = 0;
        this.orecmV = EnumBjlResult.U;
        this.orecmMoney = 0;
    }
    isUpLevel(): boolean {
        return (this.currentMoney) >= this.option.upLimit;
    }
    stop(): void {
        this.gameStart = false;
    }
    restart(reset: boolean): void {
        this.gameStart = true;
        if (reset) this.resetStrategy();
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as GroupStepAddMoneyStrategy;
        if (obj) {
            //复制
            this.currentMoney = obj.currentMoney;
            this.gameStart = obj.gameStart;

            this.recmV = obj.recmV;
            this.recmMoney = obj.recmMoney;
            this.orecmV = obj.orecmV;
            this.orecmMoney = obj.orecmMoney;
        }
    }
}